package com.joelaws.blackberry;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EmailAddressEditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.SeparatorField;

public class LoginScreen extends BaseScreen {
	private String myUserID = null;

	public LoginScreen(String myUserID) {
		super();
		this.myUserID = myUserID;
		LabelField title = new LabelField("J&P Entertainment",
				LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
		setTitle(title);

		if (myUserID == null || myUserID.equals("-1")) {
			addLoggedOutFields();
		} else {
			addLoggedInFields();
		}

	}

	private void addLoggedInFields() {
		final LabelField welcome = new LabelField("Welcome",
				LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
		add(welcome);

		ButtonField browse = new ButtonField("Browse Users");
		browse.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {

				new Thread() {
					public void run() {
						int start = 0;
						String text = getString(
								"getUserList?userID=" + myUserID + ":start="
										+ start).trim();
						String[] users = split(text, ';');
						Bitmap[] images = new Bitmap[users.length];

						for (int i = 0; i < users.length; i++) {
							images[i] = getPicture("getPicture?user="
									+ users[i]);
						}

						pushScreen(new BrowseScreen(myUserID, users, images,
								start));
					}
				}.start();
			}
		});

		add(browse);

		ButtonField meet = new ButtonField("View Meetings");
		meet.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				new Thread() {
					public void run() {
						String[] users = new String[0];

						String text = getString("getUserMeetingList?userID="
								+ myUserID);
						if (text != null && text.length() > 0
								&& !text.equals("none")) {
							users = split(text, ':');
						}

						Bitmap[] images = new Bitmap[users.length];
						for (int i = 0; i < users.length; i++) {
							images[i] = getPicture("getPicture?user="
									+ users[i]);
						}

						pushScreen(new ViewMeetingsScreen(myUserID, users,
								images));
					}
				}.start();
			}
		});

		add(meet);

		add(new SeparatorField());

		final ButtonField logout = new ButtonField("Logout");

		logout.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {

				pushScreen(new LoginScreen(null));

			}
		});
		add(logout);
	}

	private void addLoggedOutFields() {

		final EmailAddressEditField email = new EmailAddressEditField("Email:",
				"");
		add(email);

		final PasswordEditField password = new PasswordEditField("Password:",
				"");
		add(password);

		add(new SeparatorField());
		final ButtonField login = new ButtonField("Login");

		login.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				if (email.getText() == null || email.getTextLength() < 1
						|| password.getText() == null
						|| password.getTextLength() < 1) {
					return;
				}

				new Thread() {
					public void run() {

						final String myUserID = getString("login?email="
								+ email.getText() + "&pass="
								+ password.getText());
						pushScreen(new LoginScreen(myUserID));

					}
				}.start();
			}
		});
		add(login);
	}

}
